<template>
  <div class="banner-add">
    <div class="banner-add-box">
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="120px"
      >
        <el-form-item label="图片链接 link" prop="link">
          <el-input v-model="formData.link" />
        </el-form-item>
        <el-form-item prop="alt" label="图片描述 alt">
          <el-input v-model="formData.alt" />
        </el-form-item>

        <el-upload
          class="avatar-uploader"
          action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
          :show-file-list="false"
          list-type="picture"
          :http-request="handleupload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>

        <el-form-item prop="img" label="图片Base64">
          <el-input v-model="formData.img" disabled />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="handleaddbanner"
            :loading="isloading"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { addbanner } from "@/api/banner.js";
import { ElMessage } from "element-plus";
export default {
  data() {
    return {
      formData: {
        img: "",
        link: "",
        alt: "",
      },
      formRules: {
        img: [{ required: true, message: "必须" }],
        alt: [{ required: true, message: "必须" }],
        link: [{ required: true, message: "必须" }],
      },
      imageUrl: "",
      isloading: false,
    };
  },
  methods: {
    handleupload(options) {
      console.log(options);
      const file = options.file;
      //文件类型校验
      const typeright = ["image/png", "image/jpg", "image/jpeg"].includes(
        file.type
      );
      //文件大小校验
      const sizeright = file.size / 1024 / 1024 < 2;
      if (typeright) {
        if (sizeright) {
          // console.log("200");
        } else {
          ElMessage.error("校验不通过，请检查文件大小");
        }
      } else {
        ElMessage.error("校验不通过，请检查文件类型");
      }
      // 1. 创建一个 FileReader 的实例
      const fileReader = new FileReader();
      // 2. 使用 readAsDataURL 方法 获取 文件的 base64
      fileReader.readAsDataURL(file);
      // 3. 需要添加 onload 事件，读取成功之后触发
      fileReader.onload = () => {
        // 4. 获取 fileReader.result 结果
        // console.log(fileReader.result);
        // console.log(fileReader.result);
        this.imageUrl = fileReader.result;
        this.formData.img = fileReader.result;
      };
    },
    async handleaddbanner() {
      try {
        await this.$refs.formRef.validate();
        this.isloading = true;

        addbanner(this.formData)
          .then((data) => {
            console.log(data);
            this.$router.push("/banner/list");
          })
          .finally(() => {
            this.isloading = false;
          });
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped>
.banner-add {
  height: 100%;
  width: 100%;
  display: flex;
  margin: auto;
  position: relative;
  .banner-add-box {
    width: 600px;
    height: 400px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }
  .el-form {
    padding: 32px 32px 14px;
    width: 100%;
    border-radius: 10px;
    background: rgb(155, 153, 153);
  }
}
</style>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
